﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace DockDemo
{
    class Program
    {
        private static string OPEN_1 = "aa0101010d0a";
        private static string CLOSE_1 = "aa0201010d0a";

        private static string OPEN_2 = "aa0101020d0a";
        private static string CLOSE_2 = "aa0201020d0a";

        private static string OPEN_3 = "aa0101040d0a";
        private static string CLOSE_3 = "aa0201040d0a";

        private static string OPEN_4 = "aa0101080d0a";
        private static string CLOSE_4 = "aa0201080d0a";

        static void Main(string[] args)
        {
            dynamic lua = new DynamicLua.DynamicLua();
            lua.DoFile("command.lua");
            IPAddress ip = IPAddress.Parse("192.168.10.251");
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                clientSocket.Connect(new IPEndPoint(ip, 6801)); //配置服务器IP与端口  
                Console.WriteLine("连接服务器成功");
            }
            catch
            {
                Console.WriteLine("连接服务器失败，请按回车键退出！");
                return;
            }

            try
            {
                clientSocket.Send(Util.strToToHexByte(lua.open_command.four));
                Console.WriteLine("发送数据");
            }
            catch
            {
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                Console.WriteLine("发送数据失败");
            }

            Console.WriteLine("任意输入字符串关闭");

            Console.Read();


            try
            {
                clientSocket.Send(Util.strToToHexByte(lua.close_command.four));
                Console.WriteLine("发送数据");
            }
            catch
            {
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                Console.WriteLine("发送数据失败");
            }

            try
            {
                byte[] result = new byte[1024];
                int receiveLength = clientSocket.Receive(result);
                Console.WriteLine("接收服务器消息：{0}", Util.byteToHexStr(result));  
            }
            catch
            {
 
            }

            Console.Read();
            
        }
    }
}
